package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.MessageSource;
import com.myzl.coal.mapper.MessageSourceMapper;
import com.myzl.coal.mapper.MessageSourceRelationMapper;
import com.myzl.coal.service.IMessageSourceRelationService;
import com.myzl.coal.service.IMessageSourceService;
import com.myzl.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息源Service业务层处理
 *
 * @author myzl
 * @date 2023-01-10
 */
@Service
public class MessageSourceServiceImpl extends ServiceImpl<MessageSourceMapper, MessageSource> implements IMessageSourceService
{
    @Autowired
    private MessageSourceMapper messageSourceMapper;
    @Autowired
    private MessageSourceRelationMapper messageSourceRelationMapper;
    @Autowired
    private IMessageSourceRelationService messageSourceRelationService;

    /**
     * 查询消息源
     *
     * @param id 消息源主键
     * @return 消息源
     */
    @Override
    public MessageSource selectMessageSourceById(Long id)
    {
        return messageSourceMapper.selectMessageSourceById(id);
    }

    /**
     * 查询消息源列表
     *
     * @param messageSource 消息源
     * @return 消息源
     */
    @Override
    public List<MessageSource> selectMessageSourceList(MessageSource messageSource)
    {
        return messageSourceMapper.selectMessageSourceList(messageSource);
    }

    /**
     * 新增消息源
     *
     * @param messageSource 消息源
     * @return 结果
     */
    @Override
    public int insertMessageSource(MessageSource messageSource)
    {
        Integer count = messageSourceMapper.selectCount(new LambdaUpdateWrapper<MessageSource>()
                .eq(MessageSource::getEventType, messageSource.getEventType()));
        if (count > 0) {
            throw new CustomException("该数据源类型重复录入");
        }
        return messageSourceMapper.insertMessageSource(messageSource);
    }

    /**
     * 修改消息源
     *
     * @param messageSource 消息源
     * @return 结果
     */
    @Override
    public int updateMessageSource(MessageSource messageSource)
    {
        Integer count = messageSourceMapper.selectCount(new LambdaUpdateWrapper<MessageSource>()
                .eq(MessageSource::getEventType, messageSource.getEventType())
                .ne(MessageSource::getId,messageSource.getId()));
        if (count > 0) {
            throw new CustomException("该数据源类型重复录入");
        }
        return messageSourceMapper.updateMessageSource(messageSource);
    }

    /**
     * 批量删除消息源
     *
     * @param ids 需要删除的消息源主键
     * @return 结果
     */
    @Override
    public int deleteMessageSourceByIds(Long[] ids)
    {
        return messageSourceMapper.deleteMessageSourceByIds(ids);
    }

    /**
     * 删除消息源信息
     *
     * @param id 消息源主键
     * @return 结果
     */
    @Override
    public int deleteMessageSourceById(Long id)
    {
        return messageSourceMapper.deleteMessageSourceById(id);
    }

}
